<?php
namespace app\back\controller;

use app\back\model\AssortModel;
use think\Request;


///分类管理
class Assort extends AdminCommon
{


    ///数据列表页面
    public function index(AssortModel $assortModel){
        $list = $assortModel->getIndexDataList();
        $page = $list->render();

        ///渲染页面
        $this->assign([
            'indexDataList'=>$list,
            'page'=>$page,
        ]);
        return view();
    }

    ///添加页面
    public function add(){
        return view();
    }
    ///添加操作
    public function addPost(Request $request,AssortModel $assortModel){
        $assort_title = $request->param('class_title','');
        $assort_description = $request->param('class_description','');
        $assort_sort = $request->param('class_sort',0);

        ///效验
        if(!$request->isAjax()){return think_json_encode(['status'=>2,'msg'=>'增加已关闭！']);}
        if(empty($assort_title)){return think_json_encode(['status'=>2,'msg'=>'分类标题不能为空啊！']);}

        ///入库操作
        try{
            ///标题唯一效验
            $check_class_title_count = $assortModel->getCountByTitle($assort_title);
            if($check_class_title_count > 0){return think_json_encode(['status'=>2,'msg'=>'此标题已经存在，换个试试吧！']);}

            ///数据入库
            $assortModel->backInsertNewAssort(compact('assort_title','assort_description','assort_sort'));
            return json_encode(['status'=>0,'msg'=>'操作成功！']);
        }catch(\Exception $e){
            trace($e->getMessage(),'error');
            trace(M()->getLastSql(),'error');
            return think_json_encode(['status'=>2,'msg'=>'增加出现异常，请检查啊！']);
        }


    }



    ///编辑页面
    public function edit(Request $request,AssortModel $assortModel){
        ///参数
        $data_id = $request->param('id',0,'intval');

        ///获取数据
        $class_data = $assortModel->getOneDataByID($data_id);
        ///渲染页面
        $this->assign([
            'class_data'=>$class_data,
            'json_class_data'=>think_json_encode($class_data),
        ]);
        return view();
    }


    ///编辑操作
    public function editPost(Request $request,AssortModel $assortModel){
        $assort_title = $request->param('class_title','');
        $assort_description = $request->param('class_description','');
        $assort_sort = $request->param('class_sort',0,'floatval');
        $assort_status = $request->param('class_status',0,'intval');
        $assort_id = $request->param('class_id',0,'intval');

        ///效验
        if(empty($assort_title)){return think_json_encode(['status'=>2,'msg'=>'分类标题不能为空啊！']);}

        ///操作数据库
        try{
            ///标题唯一效验
            $check_class_title_count = $assortModel->getCountByNeqIDAndTitle($assort_id,$assort_title);
            if($check_class_title_count > 0){return think_json_encode(['status'=>2,'msg'=>'此标题已经存在，换个试试吧！']);}

            ///修改数据
            $edit_res = $assortModel->updateDataByID($assort_id,compact('assort_title','assort_description','assort_sort','assort_status'));
            if($edit_res == 1){
                return think_json_encode(['status'=>0,'msg'=>'修改成功！']);
            }
            return think_json_encode(['status'=>2,'msg'=>'请检查，修改失败！']);
        }catch(\Exception $e){
            trace($e->getMessage(),'error');
            trace(M()->getLastSql(),'error');
            return think_json_encode(['status'=>2,'msg'=>'修改出现异常，请检查啊！']);
        }
    }





}